/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Files
{
	/// <summary>
	/// Represents a File in a Groove Files tool.
	/// </summary>
	public class File : GrooveWebServicesV12Helpers.Files.FileFolderBase
	{
		public File(GrooveWebServicesV12Helpers.Files.FilesTool i_FilesTool, GrooveWebServicesV12Helpers.Files.Folder i_ParentFolder, GrooveFilesWebService.FileDescriptor i_FileDescriptor)
			: base(i_FilesTool, i_ParentFolder, i_FileDescriptor)
		{
		}

		/// <summary>
		/// Size of the file in bytes.
		/// </summary>
		public int Size
		{
			get
			{
				return m_FileDescriptor.Size;
			}
		}

		/// <summary>
		/// URI by which this file can be launched using Local.View()
		/// </summary>
		public string LaunchURI
		{
			get
			{
				return m_FileDescriptor.LaunchURL;
			}
		}


		/// <summary>
		/// Byte array of the file contents.
		/// </summary>
		public System.Byte[] Contents
		{
			get
			{
				//  Get initialized reference to web service
				GrooveFilesWebService.GrooveFilesBase64 FilesWebService = GrooveWebServicesV12Helpers.Files.FilesTool.CreateInitializedFilesWebService
					(
					m_IdentityURL,
					this.URI,
					Context
					);

				System.DateTime BeginDateTime = System.DateTime.Now;

				//  Ask service to read contents
				System.Byte[] Contents = FilesWebService.ReadFile();

				if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
				{
					System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
					System.Diagnostics.Debug.WriteLine("FilesWebService::ReadFile, " + TimeSpan.TotalMilliseconds + " ms");
				}

				return Contents;
			}
		}
	
		/// <summary>
		/// Download this file to the local device.
		/// </summary>
		public void Download()
		{
			//  Get initialized reference to web service
			GrooveFilesWebService.GrooveFilesBase64 FilesWebService = GrooveWebServicesV12Helpers.Files.FilesTool.CreateInitializedFilesWebService
				(
				m_IdentityURL,
				this.URI,
				Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to download
			FilesWebService.Download
				(
				false,	// recursive
				true	// recursive specified
				);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("FilesWebService::Download, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}
	}
}
